﻿using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.ArcGIS;
using PlanningGIS.Engine.Controls;
using PlanningGIS.Framework;
using PlanningGIS.Util.Log;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.File
{
    /// <summary>
    /// 添加服务
    /// </summary>
    public class AddService : BaseCommand
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public AddService()
        {
            base.m_caption = "添加服务";
            base.m_toolTip = base.Caption;
        }
        
        /// <summary>
        /// 应用程序
        /// </summary>
        IApplication m_Application = null;

        /// <summary>
        /// 创建按钮
        /// </summary>
        /// <param name="hook"></param>
        public override void OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
            }
        }

        /// <summary>
        /// 点击按钮，加载数据
        /// </summary>
        public override void OnClick()
        {
            if (m_Application == null) return;
            FrmText frm = new FrmText();
            frm.Title = "添加服务";
            frm.ToolTip = "请输入Rest、WMTS访问地址：";
            if (frm.ShowDialog((m_Application as IFramework).MdiForm) != DialogResult.OK) return;
            string url = frm.Value;
            ILayer lyr = null;
            try
            {
                if (url.ToLower().Contains("rest/services")) lyr = LayerHelper.GetRESTLayer(url) as ILayer;
                else lyr = LayerHelper.GetWMTSLayer(url) as ILayer;
                if (lyr == null) return;
                (m_Application.Document as IMxDocument).AddLayer(lyr);
                (m_Application as IFramework).MapControl.Extent = lyr.AreaOfInterest;
                (m_Application as IFramework).TOCControl.Update();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrLog("加载服务失败：" + ex.Message);
            }
            finally
            {

            }
        }

        /// <summary>
        /// 图标
        /// </summary>
        public override int Bitmap
        {
            get
            {
                return base.Bitmap;
            }
        }

        /// <summary>
        /// 是否可用
        /// </summary>
        public override bool Enabled
        {
            get
            {
                return (m_Application != null);
            }
        }

    }
}
